


//
//  TravelViewController.m
//  小碗旅行
//
//  Created by wyzcWLB on 16/9/26.
//  Copyright © 2016年 wyzcWLB. All rights reserved.
//

#import "TravelViewController.h"
#import "TravelCell.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import "discount.h"
#import "JumpViewController.h"
#import <MJRefresh.h>
#import "typeModle.h"
#import "departure.h"
#import "times_drange.h"
#import "poiModel.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Screen_Height [UIScreen mainScreen].bounds.size.height

@interface TravelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
//接收url的数组
@property(nonatomic,strong)NSMutableArray *urlArray;
@property(nonatomic,strong)UIButton *Btn;
@property(nonatomic,strong)NSArray *titles;
@property(nonatomic,strong)UIView *BtnView;
@property(nonatomic,strong)UIView *imgView;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *data1;
@property(nonatomic,strong)NSMutableArray *data2;
@property(nonatomic,strong)NSMutableArray *data3;
@property(nonatomic,strong)NSMutableArray *data4;
@end

@implementation TravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topnav"]forBarMetrics:UIBarMetricsDefault];
   
    [self creatBtn];
    
    [self createCollectionView];
    
    [self reloadData];
    
}
-(void)reloadData{
    
    
    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&is_show_pay=1&is_show_supplier=1&lat=40.03616355444207&lon=116.3638803295888&max_id=0&page=1&page_size=20&product_type=0&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&v=1";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"][@"lastminutes"];
        for (NSDictionary *dic in arr) {
            discount *model = [discount discountWithDict:dic];
            [self.dataArray addObject:model];
        }
        
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
        NSLog(@"%@",error);
    }];
    
}
-(void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 94, Screen_Width, Screen_Height-94) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView  registerNib:[UINib nibWithNibName:@"TravelCell" bundle:nil] forCellWithReuseIdentifier:@"TravelCell"];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TravelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelCell" forIndexPath:indexPath];
    
    discount *model = self.dataArray[indexPath.row];
    
    cell.title.text = model.title;
    
    cell.timeData.text = model.departureTime;
    NSString *string = model.price;
    NSRange startRange = [string rangeOfString:@"m>"];
    NSRange endRange = [string rangeOfString:@"</"];
    NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
    NSString *result = [string substringWithRange:range];
    cell.price.text = [NSString stringWithFormat:@"%@元起", result];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    cell.imgView.tag = self.dataArray.count;
    //剪圆角
    cell.imgView.layer.cornerRadius = 10;
    cell.imgView.layer.masksToBounds = YES;
    cell.imgView.userInteractionEnabled = YES;
    _urlArray = [NSMutableArray array];
    [_urlArray addObject:model.url];
    return cell;
}
#pragma mark  点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    discount *model = self.dataArray[indexPath.row];
    JumpViewController *jump =[[JumpViewController alloc]init];
    jump.url = model.url;
    [self presentViewController:jump animated:YES completion:nil];
    
}
//cell的尺寸设置
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(Screen_Width/2-5, Screen_Height*0.4);
    
}
//--------------第三页面上的四个按钮事件---------------------------
-(void)creatBtn{
    
    _BtnView = [[UIView alloc]init];
    _BtnView.frame = CGRectMake(0, 64, Screen_Width, 30);
    _BtnView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_BtnView];
    self.titles = @[@"全部类型",@"出发地",@"目的地",@"旅行时间"];
    for (int i=0; i<self.titles.count; i++) {
        CGFloat margin = 10;
        CGFloat w = (Screen_Width-5*margin)/4;
        CGFloat h = 30;
        _Btn = [[UIButton alloc]initWithFrame:CGRectMake((margin+w)*i+margin, 0, w, h)];
        [_Btn setTitle:self.titles[i] forState:UIControlStateNormal];
        _Btn.tag = i;
        [self.BtnView addSubview:_Btn];
        
        self.imgView = [[UIView alloc]initWithFrame:CGRectMake((margin+w)*i+margin+w, 12, 8, 5)];
        _imgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"expandableImage"]];
        [self.BtnView addSubview:_imgView];
        
        
        [_Btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)click:(UIButton *)sender{
    [_tableView removeFromSuperview];
    sender.selected = !sender.selected;
    self.Btn = sender;
   
    if (sender.selected) {
        
        [self creatTabelView];
            
    }else{
            
        [_tableView removeFromSuperview];
    }
    
}

-(void)creatTabelView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 94, Screen_Width, Screen_Height*0.25)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    if (_Btn.tag==0)
    {
        [self reloadData1];
        
    }else if (_Btn.tag==1)
    {
        [self reloadData2];
        
    }else if (_Btn.tag==2)
    {
        [self reloadData3];
        
    }else
    {
        [self reloadData4];
    }

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_Btn.tag==0)
    {
        return self.data1.count;
    }
    else if (_Btn.tag==1)
    {
        return self.data2.count;
    }
    else if (_Btn.tag==2)
    {
        return self.data3.count;
    }
    else
        return self.data4.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
        
    }
    if (_Btn.tag==0) {
        typeModle *model = self.data1[indexPath.row];
        cell.textLabel.text = model.catename;
        
    }else if(_Btn.tag==1){
        departure *model = self.data2[indexPath.row];
        cell.textLabel.text = model.city_des;
    }else if (_Btn.tag==2){
        poiModel *model = self.data3[indexPath.row];
        cell.textLabel.text = model.continent_name;
    }else if(_Btn.tag==3){
        times_drange *model = self.data4[indexPath.row];
        cell.textLabel.text = model.Description;
    }
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
}
-(void)reloadData1{
    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://open.qyer.com/lastminute/get_all_categorys?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&lat=40.03622967559039&lon=116.3638269096286&oauth_token=2c8a27f98da55363b7f5ca09940dc14c&page=1&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&track_user_id=6971539&type=0&v=1";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"][@"type"];
        
        for (NSDictionary *dic in arr) {
            typeModle *model = [typeModle typeModleWithDict:dic];
            
            [self.data1 addObject:model];
        }
        
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
        NSLog(@"%@",error);
    }];
    
}
-(void)reloadData2{
    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://open.qyer.com/lastminute/get_all_categorys?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&lat=40.03622967559039&lon=116.3638269096286&oauth_token=2c8a27f98da55363b7f5ca09940dc14c&page=1&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&track_user_id=6971539&type=0&v=1";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"][@"departure"];
        
        for (NSDictionary *dic in arr) {
            departure *model = [departure deparureModleWithDict:dic];
            
            [self.data2 addObject:model];
        }
        
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
        NSLog(@"%@",error);
    }];

}
-(void)reloadData3{
    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://open.qyer.com/lastminute/get_all_categorys?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&lat=40.03622967559039&lon=116.3638269096286&oauth_token=2c8a27f98da55363b7f5ca09940dc14c&page=1&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&track_user_id=6971539&type=0&v=1";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"][@"poi"];
        
        for (NSDictionary *dic in arr) {
            poiModel *model = [poiModel poiModelinitWithDict:dic];
            
            [self.data3 addObject:model];
        }
        
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
        NSLog(@"%@",error);
    }];
    
}

-(void)reloadData4{
    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://open.qyer.com/lastminute/get_all_categorys?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&lat=40.03622967559039&lon=116.3638269096286&oauth_token=2c8a27f98da55363b7f5ca09940dc14c&page=1&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&track_user_id=6971539&type=0&v=1";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"][@"times_drange"];
        
        for (NSDictionary *dic in arr) {
            times_drange *model = [times_drange timesModleWithDict:dic];
            
            [self.data4 addObject:model];
        }
        
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
        NSLog(@"%@",error);
    }];
    
}
//--------------------------第三页面四个按钮结束--------------------------
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)data1{
    if (!_data1) {
        _data1 = [NSMutableArray array];
    }
    return _data1;
}
-(NSMutableArray *)data2{
    if (!_data2) {
        _data2 = [NSMutableArray array];
    }
    return _data2;
}
-(NSMutableArray *)data3{
    if (!_data3) {
        _data3 = [NSMutableArray array];
    }
    return _data3;
}
-(NSMutableArray *)data4{
    if (!_data4) {
        _data4 = [NSMutableArray array];
    }
    return _data4;
}
@end
